package com.zjut.zs.cj.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zjut.an.util.module.PageModule;
import com.zjut.zs.userinfo.dao.ZsUserinfo;

/**
 * A data access object (DAO) providing persistence and search support for ZsCj
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.zjut.zs.cj.dao.ZsCj
 * @author MyEclipse Persistence Tools
 */

public class ZsCjDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(ZsCjDAO.class);
	// property constants
	public static final String CJ_ID = "cjId";
	public static final String US_ID = "usId";
	public static final String CJ_YW1 = "cjYw1";
	public static final String CJ_YW2 = "cjYw2";
	public static final String CJ_YW3 = "cjYw3";
	public static final String CJ_YW4 = "cjYw4";
	public static final String CJ_YW5 = "cjYw5";
	public static final String CJ_WY1 = "cjWy1";
	public static final String CJ_WY2 = "cjWy2";
	public static final String CJ_WY3 = "cjWy3";
	public static final String CJ_WY4 = "cjWy4";
	public static final String CJ_WY5 = "cjWy5";
	public static final String CJ_SX1 = "cjSx1";
	public static final String CJ_SX2 = "cjSx2";
	public static final String CJ_SX3 = "cjSx3";
	public static final String CJ_SX4 = "cjSx4";
	public static final String CJ_SX5 = "cjSx5";
	public static final String CJ_ZZ1 = "cjZz1";
	public static final String CJ_ZZ2 = "cjZz2";
	public static final String CJ_ZZ3 = "cjZz3";
	public static final String CJ_ZZ4 = "cjZz4";
	public static final String CJ_ZZ5 = "cjZz5";
	public static final String CJ_LS1 = "cjLs1";
	public static final String CJ_LS2 = "cjLs2";
	public static final String CJ_LS3 = "cjLs3";
	public static final String CJ_LS4 = "cjLs4";
	public static final String CJ_LS5 = "cjLs5";
	public static final String CJ_DL1 = "cjDl1";
	public static final String CJ_DL2 = "cjDl2";
	public static final String CJ_DL3 = "cjDl3";
	public static final String CJ_DL4 = "cjDl4";
	public static final String CJ_DL5 = "cjDl5";
	public static final String CJ_WL1 = "cjWl1";
	public static final String CJ_WL2 = "cjWl2";
	public static final String CJ_WL3 = "cjWl3";
	public static final String CJ_WL4 = "cjWl4";
	public static final String CJ_WL5 = "cjWl5";
	public static final String CJ_HX1 = "cjHx1";
	public static final String CJ_HX2 = "cjHx2";
	public static final String CJ_HX3 = "cjHx3";
	public static final String CJ_HX4 = "cjHx4";
	public static final String CJ_HX5 = "cjHx5";
	public static final String CJ_SW1 = "cjSw1";
	public static final String CJ_SW2 = "cjSw2";
	public static final String CJ_SW3 = "cjSw3";
	public static final String CJ_SW4 = "cjSw4";
	public static final String CJ_SW5 = "cjSw5";
	public static final String CJ_XX1 = "cjXx1";
	public static final String CJ_XX2 = "cjXx2";
	public static final String CJ_XX3 = "cjXx3";
	public static final String CJ_XX4 = "cjXx4";
	public static final String CJ_XX5 = "cjXx5";
	public static final String CJ_NJ1 = "cjNj1";
	public static final String CJ_NJ2 = "cjNj2";
	public static final String CJ_NJ3 = "cjNj3";
	public static final String CJ_NJ4 = "cjNj4";
	public static final String CJ_NJ5 = "cjNj5";

	protected void initDao() {
		// do nothing
	}

	public void sort(Criteria criteria, String sort, String order) {
		if (sort!=null && order!=null) {
			if (order.equals("asc")) {
				criteria.addOrder(Order.asc(sort));
			} else {
				criteria.addOrder(Order.desc(sort));
			}
		} else {
			criteria.addOrder(Order.desc(CJ_ID));			
		}
	}
	
	public void save(ZsCj transientInstance) {
		log.debug("saving ZsCj instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(ZsCj transientInstance) {
		log.debug("updating ZsCj instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public void delete(ZsCj persistentInstance) {
		log.debug("deleting ZsCj instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZsCj findById(java.lang.Integer id) {
		log.debug("getting ZsCj instance with id: " + id);
		try {
			ZsCj instance = (ZsCj) getHibernateTemplate().get(
					"com.zjut.zs.cj.dao.ZsCj", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZsCj instance) {
		log.debug("finding ZsCj instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZsCj instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZsCj as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCjYw1(Object cjYw1) {
		return findByProperty(CJ_YW1, cjYw1);
	}

	public List findByCjYw2(Object cjYw2) {
		return findByProperty(CJ_YW2, cjYw2);
	}

	public List findByCjYw3(Object cjYw3) {
		return findByProperty(CJ_YW3, cjYw3);
	}

	public List findByCjYw4(Object cjYw4) {
		return findByProperty(CJ_YW4, cjYw4);
	}

	public List findByCjYw5(Object cjYw5) {
		return findByProperty(CJ_YW5, cjYw5);
	}

	public List findByCjWy1(Object cjWy1) {
		return findByProperty(CJ_WY1, cjWy1);
	}

	public List findByCjWy2(Object cjWy2) {
		return findByProperty(CJ_WY2, cjWy2);
	}

	public List findByCjWy3(Object cjWy3) {
		return findByProperty(CJ_WY3, cjWy3);
	}

	public List findByCjWy4(Object cjWy4) {
		return findByProperty(CJ_WY4, cjWy4);
	}

	public List findByCjWy5(Object cjWy5) {
		return findByProperty(CJ_WY5, cjWy5);
	}

	public List findByCjSx1(Object cjSx1) {
		return findByProperty(CJ_SX1, cjSx1);
	}

	public List findByCjSx2(Object cjSx2) {
		return findByProperty(CJ_SX2, cjSx2);
	}

	public List findByCjSx3(Object cjSx3) {
		return findByProperty(CJ_SX3, cjSx3);
	}

	public List findByCjSx4(Object cjSx4) {
		return findByProperty(CJ_SX4, cjSx4);
	}

	public List findByCjSx5(Object cjSx5) {
		return findByProperty(CJ_SX5, cjSx5);
	}

	public List findByCjZz1(Object cjZz1) {
		return findByProperty(CJ_ZZ1, cjZz1);
	}

	public List findByCjZz2(Object cjZz2) {
		return findByProperty(CJ_ZZ2, cjZz2);
	}

	public List findByCjZz3(Object cjZz3) {
		return findByProperty(CJ_ZZ3, cjZz3);
	}

	public List findByCjZz4(Object cjZz4) {
		return findByProperty(CJ_ZZ4, cjZz4);
	}

	public List findByCjZz5(Object cjZz5) {
		return findByProperty(CJ_ZZ5, cjZz5);
	}

	public List findByCjLs1(Object cjLs1) {
		return findByProperty(CJ_LS1, cjLs1);
	}

	public List findByCjLs2(Object cjLs2) {
		return findByProperty(CJ_LS2, cjLs2);
	}

	public List findByCjLs3(Object cjLs3) {
		return findByProperty(CJ_LS3, cjLs3);
	}

	public List findByCjLs4(Object cjLs4) {
		return findByProperty(CJ_LS4, cjLs4);
	}

	public List findByCjLs5(Object cjLs5) {
		return findByProperty(CJ_LS5, cjLs5);
	}

	public List findByCjDl1(Object cjDl1) {
		return findByProperty(CJ_DL1, cjDl1);
	}

	public List findByCjDl2(Object cjDl2) {
		return findByProperty(CJ_DL2, cjDl2);
	}

	public List findByCjDl3(Object cjDl3) {
		return findByProperty(CJ_DL3, cjDl3);
	}

	public List findByCjDl4(Object cjDl4) {
		return findByProperty(CJ_DL4, cjDl4);
	}

	public List findByCjDl5(Object cjDl5) {
		return findByProperty(CJ_DL5, cjDl5);
	}

	public List findByCjWl1(Object cjWl1) {
		return findByProperty(CJ_WL1, cjWl1);
	}

	public List findByCjWl2(Object cjWl2) {
		return findByProperty(CJ_WL2, cjWl2);
	}

	public List findByCjWl3(Object cjWl3) {
		return findByProperty(CJ_WL3, cjWl3);
	}

	public List findByCjWl4(Object cjWl4) {
		return findByProperty(CJ_WL4, cjWl4);
	}

	public List findByCjWl5(Object cjWl5) {
		return findByProperty(CJ_WL5, cjWl5);
	}

	public List findByCjHx1(Object cjHx1) {
		return findByProperty(CJ_HX1, cjHx1);
	}

	public List findByCjHx2(Object cjHx2) {
		return findByProperty(CJ_HX2, cjHx2);
	}

	public List findByCjHx3(Object cjHx3) {
		return findByProperty(CJ_HX3, cjHx3);
	}

	public List findByCjHx4(Object cjHx4) {
		return findByProperty(CJ_HX4, cjHx4);
	}

	public List findByCjHx5(Object cjHx5) {
		return findByProperty(CJ_HX5, cjHx5);
	}

	public List findByCjSw1(Object cjSw1) {
		return findByProperty(CJ_SW1, cjSw1);
	}

	public List findByCjSw2(Object cjSw2) {
		return findByProperty(CJ_SW2, cjSw2);
	}

	public List findByCjSw3(Object cjSw3) {
		return findByProperty(CJ_SW3, cjSw3);
	}

	public List findByCjSw4(Object cjSw4) {
		return findByProperty(CJ_SW4, cjSw4);
	}

	public List findByCjSw5(Object cjSw5) {
		return findByProperty(CJ_SW5, cjSw5);
	}

	public List findByCjXx1(Object cjXx1) {
		return findByProperty(CJ_XX1, cjXx1);
	}

	public List findByCjXx2(Object cjXx2) {
		return findByProperty(CJ_XX2, cjXx2);
	}

	public List findByCjXx3(Object cjXx3) {
		return findByProperty(CJ_XX3, cjXx3);
	}

	public List findByCjXx4(Object cjXx4) {
		return findByProperty(CJ_XX4, cjXx4);
	}

	public List findByCjXx5(Object cjXx5) {
		return findByProperty(CJ_XX5, cjXx5);
	}

	public List findByCjNj1(Object cjNj1) {
		return findByProperty(CJ_NJ1, cjNj1);
	}

	public List findByCjNj2(Object cjNj2) {
		return findByProperty(CJ_NJ2, cjNj2);
	}

	public List findByCjNj3(Object cjNj3) {
		return findByProperty(CJ_NJ3, cjNj3);
	}

	public List findByCjNj4(Object cjNj4) {
		return findByProperty(CJ_NJ4, cjNj4);
	}

	public List findByCjNj5(Object cjNj5) {
		return findByProperty(CJ_NJ5, cjNj5);
	}

	public List findAll() {
		log.debug("finding all ZsCj instances");
		try {
			String queryString = "from ZsCj";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZsCj merge(ZsCj detachedInstance) {
		log.debug("merging ZsCj instance");
		try {
			ZsCj result = (ZsCj) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZsCj instance) {
		log.debug("attaching dirty ZsCj instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZsCj instance) {
		log.debug("attaching clean ZsCj instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ZsCjDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ZsCjDAO) ctx.getBean("ZsCjDAO");
	}
	
	@SuppressWarnings("unchecked")
	public PageModule findByInstance(final int pageNumber ,final int pageSize, String sort, String order, final ZsCj obj){
		try {
			int startIndex = (pageNumber - 1) * pageSize;
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			List list;
			Criteria criteria = session.createCriteria(ZsCj.class);
			
			if (null != obj.getZsUser()) {
				criteria.add(Restrictions.eq("zsUser", obj.getZsUser()));
			}
			//补充
			
			this.sort(criteria, sort, order);
			
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
			list = criteria.list();
			session.close();
			return new PageModule(pageNumber, pageSize, countByInstance(obj), list);
		} catch (RuntimeException re) {
			log.error("findByInstance failed", re);
			throw re;
		}
	}
	
	public int countByInstance(final ZsCj obj) {
		log.debug("finding the number of ZsCj instance");
		try {
			int count = 0;
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(ZsCj.class);
			
			if (null != obj.getZsUser()) {
				criteria.add(Restrictions.eq("zsUser", obj.getZsUser()));
			}
			//补充
			
			count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
			session.close();
			return count;
		} catch (RuntimeException re) {
			log.error("find the number of ZsCj instance failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List findByHql(final int pageNumber ,final int pageSize, final String hql, final Object[] values){
		log.debug("finding ZsCj instance by hql");
	    return (List<ZsCj>) super.getHibernateTemplate().execute(new HibernateCallback(){
	        public Object doInHibernate(Session session) throws HibernateException, SQLException {
	            Query query=session.createQuery(hql);
	            if (values != null) {// 为hql语句传入参数
					for (int i = 0; i < values.length; i++) {
						query.setParameter(i, values[i]);
					}
				}
	            return query.setFirstResult((pageNumber-1)*pageSize).setMaxResults(pageSize).list();
	        }
	    });
	}

	@SuppressWarnings("unchecked")
	public int findCountByHql(final String hql, final Object[] values) {
		log.debug("finding the number of ZsCj instance");
		try {
			return (Integer) super.getHibernateTemplate().execute(new HibernateCallback(){
		        public Object doInHibernate(Session session) throws HibernateException, SQLException {
		            Query query=session.createQuery(hql);
		            if (values != null) {// 为hql语句传入参数
						for (int i = 0; i < values.length; i++) {
							query.setParameter(i, values[i]);
						}
					}
					return ((Long)query.uniqueResult()).intValue();
		        }
		    });
		} catch (RuntimeException re) {
			log.error("find the number of ZsCj instance failed", re);
			throw re;
		}
	}
	
	private boolean isEmpty(String value) {
		if (value==null || value.trim().length()==0) {
			return true;
		}
		return false;
	}
}